package com.example.zjx8969.myapplication.dialogManager

import android.util.Log
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class QueueDialogModel(private val coroutineScope: CoroutineScope, private val array: Array<Class<out QueuedDialogTask<*>>>) {
    private var currentIndex = 0

    fun showNextDialogByOrder() {
        coroutineScope.launch(Dispatchers.Default) {
            //初次延迟1.5s，等待UI组件初始化
            if (currentIndex < 0) delay(1500)

            while (currentIndex < array.size) {
                val queuedDialogTask: Class<out QueuedDialogTask<*>> = array[currentIndex]
                Log.d("queueDialog", "show index=$currentIndex, className=${queuedDialogTask.canonicalName}")
                val instance = queuedDialogTask.getDeclaredConstructor(QueueDialogModel::class.java)
                    .newInstance(this@QueueDialogModel)
                instance.showNextAvailableDialog()
                currentIndex++
            }

        }
    }

}